# 分别使用urllib库和request库的相关功能，访问网址下列网址
# https://bj.fang.com/quanwangso/search.html?city=bj&refer=sy_seach
# https://www.guazi.com/bj/buy/
# 将上述访问结果中的状态码、返回对象属性、返回体进行调试查看或者打印、保存下来
# 该部分为request库的代码
import requests
# 网址
url1 = 'https://bj.fang.com/quanwangso/search.html?city=bj&refer=sy_seach'
url2 = 'https://www.guazi.com/bj/buy/'

def fetch_url_with_requests(url):
    try:
        response = requests.get(url)  
        # 获取状态码
        status_code = response.status_code
        # 获取返回对象属性
        headers = response.headers
        # 读取返回体
        body = response.text
        
        # 打印
        print(f"URL: {url}")
        print(f"Status Code: {status_code}")
        print(f"Headers: {headers}")
        print(f"Body (first 1000 characters): {body[:1000]}...\n")  # 只打印前1000个字符避免过长
        
        # 保存返回体到文件（可选）
        with open(f'{requests.compat.urlparse(url).netloc}_response_body.txt', 'w', encoding='utf-8') as f:
            f.write(body)
    
    except Exception as e:
        print(f"Error fetching {url}: {e}")

# 访问网址
fetch_url_with_requests(url1)
fetch_url_with_requests(url2)